home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Technotools
/
Technotools (Chestnut CD-ROM)(1993).ISO
/
lang_c
/
msqc25t1
/
lstsdir.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-09-07
|
3KB
|
103 lines
/* where.c: Searches directory structure from root and list all occurrences
of a filename matching the search argument from the command line.
*/
#include <stdio.h>
#include <dos.h>
#include <direct.h>
#include <conio.h>
#include <string.h>
#include <malloc.h>
/* Global to count matches found */
int count = 0;
main( int argc, char *argv[])
{
char *curdir, /* current directory on entry */
sought[80], /* pattern to search for */
*temp; /* temporary work string */
int curdrive, newdrive, /* current and new drives */
p, n = 4; /* misc counters */
void searchdir (char *dir, char *ptrn); /* search fcn */
for(p=0;p<80;p++)
sought[p] = 0;
/* Find out where we are */
curdir = getcwd (NULL, 80);
_dos_getdrive (&curdrive);
/* Find out what we're looking for */
if (argc > 1)
strcpy (sought, argv[1]); /* patten from cmd line */
/* Get designator for another drive if specified */
if (sought[1] == ':') {
newdrive = (toupper (sought[0])) - 64; /* convert */
_dos_setdrive (newdrive, &n); /* set new drive */
p = (sought[2] == '\\') ? 3 : 2; /* start of new pattern */
strcpy (sought, &(sought[p])); /* take out drive */
}
else {
_dos_getdrive (&newdrive);
}
/* Add wildcard prefix/suffix if necessary */
temp = strcat (".", sought); /* set prefix */
strcpy (sought, temp);
/* Perform search for pattern starting in root */
searchdir ("\\", sought);
printf("\nTotal of %d subdirectories on drive %c:", count, newdrive + 64);
/* Restore orginal drive and directory */
_dos_setdrive (curdrive, &n);
chdir (curdir);
}
void searchdir (char *path, char *ptrn)
/* recursive directory search routine */
#define ANYFILE 0xFF /* wildcard attribute for any file */
{
struct find_t *f;
char *wholepath;
unsigned rtn;
chdir (path); /* change to new directory */
wholepath = getcwd (NULL, 80); /* get full pathname */
printf("%s", wholepath);
if(wholepath[strlen(wholepath)-1] == '\\')
printf("\n");
else printf("\\\n");
f = malloc (sizeof (*f));
/*search for filename matches in this directory */
rtn = _dos_findfirst (ptrn, ANYFILE, f);
while (rtn == 0) { /* list path */
++count;
rtn = _dos_findnext (f); /* find next match */
}
/* Now search any subdirectories under the directory */
rtn = _dos_findfirst ("*.*", _A_SUBDIR, f);
while (rtn == 0) {
if ((f->attrib == _A_SUBDIR) && (f->name[0] != '.')) {
searchdir (f->name, ptrn); /* recursive call */
chdir (wholepath); /* back to this dir */
}
rtn = _dos_findnext (f); /* search next dir */
}
/* Free allocated space */
free (wholepath);
free (f);
}